package com.js.nickcar.module.addcar.baseinfo;

import android.support.annotation.NonNull;

import com.js.nickcar.api.HttpHelpter;
import com.js.nickcar.api.OptionsHelper;
import com.js.nickcar.api.UrlConfig;
import com.js.nickcar.api.UrlUtils;
import com.js.nickcar.base.ApiCallback;
import com.js.nickcar.base.BasePresenter;
import com.js.nickcar.base.HttpResultFunc;
import com.js.nickcar.entity.local.BaseCarInfoEntity;
import com.js.nickcar.entity.remote.ApiResultBean;
import com.js.nickcar.entity.remote.OptionBean;
import com.zyzd.commontools.log.TLog;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;

import static com.js.nickcar.api.OptionConfig.CAR_INFO_TRANS_TIMES;
import static com.js.nickcar.api.OptionConfig.CAR_INFO_USAGE;
import static com.js.nickcar.api.OptionsHelper.CAR_INFO_SUNROOF;

/**
 * Created by LZY on 2017/9/28.
 */
public class BaseCarInfoPresenter extends BasePresenter<BaseCarInfoView>
        implements BaseCarInfoEntity.ItemType, BaseCarInfoEntity.Param {

    private OptionBean OptionSunroof;
    private OptionBean OptionUsage;
    private OptionBean OptionTransTimes;
    private Date OptionDate;

    public List<BaseCarInfoEntity> getData() {
        ArrayList<BaseCarInfoEntity> lists = new ArrayList<>();
        lists.add(new BaseCarInfoEntity("所在城市", TYPE_SELECT, RECEIVE_CITY));
        lists.add(new BaseCarInfoEntity("车型", TYPE_SELECT, CAR_TYPE));
        lists.add(new BaseCarInfoEntity("车辆颜色", TYPE_INPUT, COLOR));
        lists.add(new BaseCarInfoEntity("天窗", TYPE_SELECT, SUNROOF));
        lists.add(new BaseCarInfoEntity("座位", TYPE_INPUT, SEATS));
        lists.add(new BaseCarInfoEntity("排量", TYPE_INPUT, DISPLACEMENT));
        lists.add(new BaseCarInfoEntity("行驶里程(万公里)", TYPE_INPUT, MILEAGE));
        lists.add(new BaseCarInfoEntity("首次上牌时间", TYPE_SELECT, PLATE_TIME));
        lists.add(new BaseCarInfoEntity("使用年限", TYPE_INPUT, SERVICE));
        lists.add(new BaseCarInfoEntity("使用性质", TYPE_SELECT, USAGE));
        lists.add(new BaseCarInfoEntity("发动机号", TYPE_INPUT, ENGINE_NO));
        lists.add(new BaseCarInfoEntity("过户次数", TYPE_SELECT, TRANS_TIMES));
        lists.add(new BaseCarInfoEntity("下一步", TYPE_NEXT, NEXT));
        return lists;
    }

    public ArrayList<OptionBean> getSunroofData() {
        return OptionsHelper.getOptions().get(CAR_INFO_SUNROOF);
    }

    public ArrayList<OptionBean> getUsageData() {
        return OptionsHelper.getOptions().get(CAR_INFO_USAGE);
    }

    public ArrayList<OptionBean> getTransTimesData() {
        return OptionsHelper.getOptions().get(CAR_INFO_TRANS_TIMES);
    }

    public Date getOptionDate() {
        return OptionDate;
    }


    public void setOptionDate(Date optionDate) {
        OptionDate = optionDate;
    }

    public void getOptions(String[] types) {
        StringBuilder sb = new StringBuilder();
        if (types != null && types.length > 0) {
            sb.append(types[0]);
            for (int i = 1; i < types.length; i++) {
                sb.append("|").append(types[i]);
            }
        }
        Map<String, String> baseRequestMap = UrlUtils.getBaseRequestMap(UrlConfig.API_COMMON_OPTIONS);
        baseRequestMap.put("type",sb.toString());
        Observable<ApiResultBean<HashMap<String, ArrayList<OptionBean>>>> options = HttpHelpter.getApiService().getOptions(baseRequestMap);
        addSubscription(options.map(new HttpResultFunc<HashMap<String, ArrayList<OptionBean>>>()), getOptionSubscriber());
    }

    private Subscriber<HashMap<String, ArrayList<OptionBean>>> getOptionSubscriber() {
        return new ApiCallback<HashMap<String, ArrayList<OptionBean>>>() {
            @Override
            public void onSuccess(HashMap<String, ArrayList<OptionBean>> data) {
                //获取参数信息成功，更新本地缓存，通知页面更新
                OptionsHelper.updateOption(data);
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }

            @Override
            public void onFinish() {

            }
        };
    }

    public void setOptionSunroof(OptionBean optionSunroof) {
        this.OptionSunroof = optionSunroof;
    }

    public OptionBean getOptionSunroof() {
        return this.OptionSunroof;
    }

    public OptionBean getOptionUsage() {
        return OptionUsage;
    }

    public void setOptionUsage(OptionBean optionUsage) {
        OptionUsage = optionUsage;
    }

    public OptionBean getOptionTransTimes() {
        return OptionTransTimes;
    }

    public void setOptionTransTimes(OptionBean optionTransTimes) {
        OptionTransTimes = optionTransTimes;
    }

    public void commitCarInfo(Map<String, String> map) {
        TLog.d("zyzd", "BaseCarInfoPresenter>>commitCarInfo--> " + map);
        addSubscription(HttpHelpter.getApiService()
                .commitCarBaseInfo(map)
                .map(new HttpResultFunc<String>()), getCommitCarBaseInfoSubcriber());
    }

    @NonNull
    private ApiCallback<String> getCommitCarBaseInfoSubcriber() {
        return new ApiCallback<String>() {
            @Override
            public void onSuccess(String carId) {
                TLog.d("zyzd", "BaseCarInfoPresenter>>onSuccess-carId-> " + carId);
                mView.commitCarBaseInfoSuccess(carId);
            }

            @Override
            public void onFailure(String msg) {
                TLog.d("zyzd", "BaseCarInfoPresenter>>onFailure--> " + msg);
            }

            @Override
            public void onFinish() {

            }
        };
    }
}
